
var MOD_SR={
  VIEWDOSS:[],
  objtab:[]
}




MOD_SR.onchdocrmks=function(doc,dn) {
  SetDocDBRmks(doc,dn, html_flexarea_selected().value);
}

MOD_SR.onchseldoss=function(doc) {
  MOD_SR.VIEWDOSS[doc]=html_selected("seldos"+doc);
  show_SR();
}



function show_SRNU(nustyle) {
  var nutxt="";
  var g;
  var _nu=GetGroupsLength();
  var paid=GetNumGroupsPaid();
  codeprt();	

  if(_nu <= 1) {
    return "Only one invention !";
  }
 
  if(GetClaimSetPhase()=="PCT") {
    if(paid>1) {
      nutxt+=nu_subinventionsnofee()+BR2;
    }
    nutxt+="Reasons for lack of unity :"+BR+"---------------------------"+BR;  
    
    if(MOD_NU.nustyle=="Short") {
      
      nutxt+=nu_prior_art(nustyle)+BR;
      
      nutxt+=nu_late_aposteriori()+BR; 
      nutxt+=nu_differences()+BR;
      nutxt+=nu_problems(MOD_NU.nustyle)+BR;
      nutxt+=nu_stf()+BR;
      
      nutxt+=stdcls_apply_unity("F08","","PCT","")+BR2;
      
    } else {
     
      nutxt+=nu_describe_all_stfs();
      nutxt+="Therefore, the International Searching Authority is of the opinion that ";
      nutxt+=_nu+" inventions are claimed :"+BR2;
      nutxt+=nu_inventions("noheader")+BR2;
      nutxt+=nu_problems(MOD_NU.nustyle);
      nutxt+=nu_exponential();
    }

    nutxt+=stdcls_apply_unity("conclusion","","PCT","")+BR2;
    if(paid==1) {
      nutxt+=stdcls_apply_unity("partialsr",GetGroupClaims(GetGroupName(1)),"PCT","")+BR2;
    } else {
      nutxt+=stdcls_apply_unity("partialsr",GetAllPaidClaims(),"PCT","")+BR2;
    }
    /*
    if(paid==1) {
      nutxt+="Accordingly, the partial search report ";
      nutxt+="has been drafted for the first invention (claims "+list2text(GetGroupClaims(GetGroupName(1)))+") and the "; 
      nutxt+="applicant is invited to pay "+(_nu-paid)+" additional search "+plural(_nu-paid,"fee")+"."+BR2;
    } else {
      nutxt+="Accordingly, the partial search report ";
      nutxt+="has been drafted for the inventions mentioned under item 1 ";
      nutxt+="(claims "+List2text(GetAllPaidClaims())+")";
      if(_nu-paid >0) {
	nutxt+=" and the applicant is invited to pay "+(_nu-paid)+" additional search "+plural(_nu-paid,"fee");
      }
      nutxt+="."+BR2;
    } 
    */
    nutxt+=stdcls_apply_unity("warnings","","PCT","")+BR2;
  } else {
    nutxt+=nu_inventions("noheader")+BR2;
  }
  return nutxt;
}


function show_SRFORM(opt) {
  var docarray=[];
  var html="";
  var valdocs=[];
  var validx=["XN","XI","Y","A",""];

  for(var idx in validx) {
    valdocs[validx[idx]]=[];
  }

  html+="<TABLE border=1>"; 

  InitSREntries();

  // docarray is a set with all documents having some value
  var cl_iter= new filteredclaims_iterator();   
  while(!cl_iter.end()) {
    var cl=cl_iter.getValue();
    var val;
    for(var d_iter=new doc_iterator(); !d_iter.end(); d_iter.next()) { //=1; d <= GetDocsLength(); d++) {
      var pn=d_iter.getPN();
      val=GetClaimDocVal(cl,pn,GetClaimEq(cl));
      switch(val) {
      case "XI": 
	if(GetClaimDocUsrAttr(cl,pn,"Y")) {  // Y  opt!="V" && 
	  valdocs["Y"][pn]=(valdocs["Y"][pn] || "")+(cl)+","; 
	  docarray[pn]=1;
	  break;
	}
      default:
	if(val) {
	  valdocs[val][pn]=(valdocs[val][pn] || "")+(cl)+","; 
	  docarray[pn]=1;
	}
      }
    }
    cl_iter.next();
  }

  if(!GetPrefsAttr("trimaran")) {
    validx=["X","Y","A"];
    valdocs["X"]=[];
  } else {
    validx=["XN","XI","Y","A"];
  }
  for(var doc in docarray) {
    var  cllst="";
    var idx=GetDocIdx(doc);
    var pfxlist=new List();
    if(IsDocD(idx)) {
      pfxlist.push("D");
    }
    if(IsDocP(idx)) {
      pfxlist.push("P");
    }
    if(IsDocE(idx)) {
      pfxlist.push("E");
    }

    html+="<TR><TH>Document</TH>";
    html+="<TH>Value</TH><TH> Claims</TH></TR>";
    html+="<TR><TD>"+html_whatis(doc,GetDocDBTitle(doc))+"</TD></TR>";
    
    if(!GetPrefsAttr("trimaran")) {
      valdocs["X"][doc]=valdocs["XN"][doc]+valdocs["XI"][doc];
    } 

    for(var idx in validx) {
      cllst=rmlastchar(valdocs[validx[idx]][doc] || ",");
      html+="<TR><TD></TD><TD align=center>"+pfxlist.getValue()+validx[idx]+"</TD><TD>";
      html+="<input type=input readonly size=80 value='"+List_tolist(cllst)+"'>";
      html+="</TD></TR>";     
    }
   
    var htdbg="";
    var check_done= new Array();
    var cl_iter= new filteredclaims_iterator();   
    while(!cl_iter.end()) {
      var cl=cl_iter.getValue();    
      var tf_iter= new claim_features_iterator(cl);
      while(!tf_iter.end()) {       
	// direct passage does not work if referenced feature is not in showned claim list
	var pass=tf_iter.getPassageEq(doc);
	if(pass && !check_done[pass]) {
	  //var fpass=filter_hints(pass); // back compat with old hints 	
	  //if(fpass) {
	  parse_passage(doc,pass); 
	  //htdbg+="<TR><TD></TD><TD colspan=2><span class='warning'>DBG</span><input type=input size=90 readonly value='"+pass+"'></TD></TR>";
	  //}
	  check_done[pass]=1;
	}	   
	tf_iter.next();
      }
      cl_iter.next();
    }
    html+="<TR><TH></TH><TH></TH><TH>Passages</TH></TR>";
    html+="<TR><TD></TD><TD>Figures   </TD><TD><input type=input size=80 readonly value='"+srfigures(doc)+"'></TD></TR>";
    html+="<TR><TD></TD><TD>Claims    </TD><TD><input type=input size=80 readonly value='"+List_tolist(srclaims(doc))+"'></TD></TR>";
    html+="<TR><TD></TD><TD>Tables    </TD><TD><input type=input size=80 readonly value='"+srtabs(doc)+"'></TD></TR>";
    html+="<TR><TD></TD><TD>Paragraphs</TD><TD><input type=input size=80 readonly value='"+srparagraphs(doc)+"'></TD></TR>";
    html+="<TR><TD></TD><TD>Pages/Cols</TD><TD>"+html_flexarea(srrefs(doc),"","readonly bgcolor=white",50)+"</TD></TR>";

    if(!MOD_SR.VIEWDOSS[doc]) {
      MOD_SR.VIEWDOSS[doc]=GetDossierNumber();
    }

    html+="<TR><TH></TH><TH></TH><TH>Remarks</TH></TR>";
    html+="<TR><TD valign=top colspan=2 align=right> "
    var docrmks=GetDocDBRmksDossiers(doc,GetDossierNumber());
    if(docrmks.length>1) {
      html+="Dossier ";
      html+=html_selection("seldos"+doc,
			   docrmks,
			   MOD_SR.VIEWDOSS[doc],
			   "MOD_SR.onchseldoss(\""+doc+"\")");
    }
    html+="</TD>";
    html+="<TD>"+html_flexarea(GetDocDBRmks(doc,MOD_SR.VIEWDOSS[doc]),
			       "MOD_SR.onchdocrmks(\\\""+doc+"\\\",\\\""+MOD_SR.VIEWDOSS[doc]+"\\\")"
			       ,"",50)+"</TD></TR>";
    /*
    if(dbgview) {
      html+=htdbg;
    }
    */
  } 
  html+="</TABLE>";
  return html;
}


MOD_SR.objtab["SR"]={ label:"Search Report", callback: function () { return show_SRFORM("V");}};
MOD_SR.objtab["NU"]={ label:"Non Unity", callback: function() {
    MOD_NU.nustyle=html_selected("nustyle",MOD_NU.NUSTYLES,MOD_NU.nustyle);
    var html="<h3>Reasoning "+html_selection("nustyle",MOD_NU.NUSTYLES,MOD_NU.nustyle,"show_SR()")+"</h3>";
    html+=comm_area(show_SRNU(MOD_NU.nustyle));
    return html;
  }
}

function show_SR() {
  var html="";
  //InitFilters("nopages");
 
  MOD_SR.objtab["NU"].disabled=GetGroupsLength()<=1;
  
  ClaimToolCommSection("Search Report",
		       html_tabs_obj("srview",MOD_SR.objtab),
		       "show_SR()");
}
